<template>
  <div>
    <!--页面区域-->
    <div class="page-view">
      <div class="table-operations">
        <a-space>
          <a-button type="primary" @click="handleAdd">新增</a-button>
          <a-button @click="handleBatchDelete">批量删除</a-button>
          <a-input-search addon-before="用户名" enter-button @search="onSearch" @change="onSearchChange" />
        </a-space>
      </div>
      <a-table
        size="middle"
        rowKey="id"
        :loading="data.loading"
        :columns="columns"
        :data-source="data.userList"
        :scroll="{ x: 'max-content' }"
        :row-selection="rowSelection"
        :pagination="{
          size: 'default',
          current: data.page,
          pageSize: data.pageSize,
          onChange: onChange,
          showSizeChanger: false,
          showTotal: (total) => `共${total}条数据`,
        }"
      >
        <template #bodyCell="{ text, record, column }">
          <template v-if="column.key === 'role'">
            <span>
              <span v-if="text === '1'">管理员</span>
              <span v-if="text === '2'">普通用户</span>
              <span v-if="text === '3'">演示账号</span>
            </span>
          </template>
          <template v-if="column.key === 'operation'">
            <span>
              <a @click="handleEdit(record)">编辑</a>
              <a-divider type="vertical" />
              <a-popconfirm title="确定删除?" ok-text="是" cancel-text="否" @confirm="confirmDelete(record)">
                <a href="#">删除</a>
              </a-popconfirm>
            </span>
          </template>
        </template>
      </a-table>
    </div>

    <!--弹窗区域-->
    <div>
      <a-modal
        :visible="modal.visible"
        :forceRender="true"
        :title="modal.title"
        ok-text="确认"
        cancel-text="取消"
        @cancel="handleCancel"
        @ok="handleOk"
      >
        <div>
          <a-form ref="myform" :label-col="{ style: { width: '80px' } }" :model="modal.form" :rules="modal.rules">
            <a-row :gutter="24">
              <a-col span="24">
                <a-form-item label="用户名" name="username">
                  <a-input :disabled="modal.editFlag" placeholder="请输入" v-model:value="modal.form.username" allowClear />
                </a-form-item>
              </a-col>
              <a-col span="24" v-if="!modal.editFlag">
                <a-form-item label="密码" name="password">
                  <a-input placeholder="请输入" type="password" v-model:value="modal.form.password" allowClear />
                </a-form-item>
              </a-col>
              <a-col span="24">
                <a-form-item label="昵称" name="nickname">
                  <a-input placeholder="请输入" v-model:value="modal.form.nickname" allowClear />
                </a-form-item>
              </a-col>
              <a-col span="24">
                <a-form-item label="角色" name="role">
                  <a-select placeholder="请选择" allowClear v-model:value="modal.form.role">
                    <template v-for="item in modal.roleData" :key="item.id">
                      <a-select-option :value="item.id">{{ item.title }}</a-select-option>
                    </template>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col span="24">
                <a-form-item label="状态" name="status">
                  <a-select placeholder="请选择" allowClear v-model:value="modal.form.status">
                    <a-select-option key="0" value="0">正常</a-select-option>
                    <a-select-option key="1" value="1">封号</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col span="24">
                <a-form-item label="邮箱" name="email">
                  <a-input placeholder="请输入" v-model:value="modal.form.email" allowClear />
                </a-form-item>
              </a-col>
              <a-col span="24">
                <a-form-item label="手机号" name="mobile">
                  <a-input placeholder="请输入" v-model:value="modal.form.mobile" allowClear />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </div>
      </a-modal>
    </div>
  </div>
</template>

<script setup lang="ts">
  import { ref, reactive, onMounted } from 'vue';
  import { FormInstance, message } from 'ant-design-vue';
  import { createApi, listApi, updateApi, deleteApi } from '/@/api/user';
  import { getFormatTime } from '/@/utils';
  import type { ColumnType } from 'ant-design-vue/es/table';

  interface DataItem {
    index: number;
    id: string | number;
    username: string;
    nickname: string;
    role: string;
    status: string;
    email: string;
    mobile: string;
    create_time: string;
  }

  const columns = reactive<ColumnType[]>([
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      align: 'center',
    },
    {
      title: '用户名',
      dataIndex: 'username',
      key: 'username',
      align: 'center',
    },
    {
      title: '昵称',
      dataIndex: 'nickname',
      key: 'nickname',
      align: 'center',
    },
    {
      title: '角色',
      dataIndex: 'role',
      key: 'role',
      align: 'center',
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      align: 'center',
      customRender: ({ text }: { text: string }) => (text === '0' ? '正常' : '封号'),
    },
    {
      title: '邮箱',
      dataIndex: 'email',
      key: 'email',
      align: 'center',
    },
    {
      title: '手机号',
      dataIndex: 'mobile',
      key: 'mobile',
      align: 'center',
    },
    {
      title: '创建时间',
      dataIndex: 'create_time',
      key: 'create_time',
      align: 'center',
    },
    {
      title: '操作',
      dataIndex: 'action',
      key: 'operation',
      align: 'center',
      fixed: 'right',
      width: 140,
    },
  ]);

  const beforeUpload = (file: File) => {
    const fileName = new Date().getTime().toString() + '.' + file.type.substring(6);
    const copyFile = new File([file], fileName);
    modal.form.cover = copyFile;
    fileList.value = [copyFile];
    return false;
  };

  const fileList = ref<File[]>([]);

  const data = reactive({
    userList: [] as DataItem[],
    loading: false,
    keyword: '',
    selectedRowKeys: [] as (string | number)[],
    pageSize: 10,
    page: 1,
  });

  const modal = reactive({
    visible: false,
    editFlag: false,
    title: '',
    roleData: [
      { id: '1', title: '管理员' },
      { id: '2', title: '普通用户' },
      { id: '3', title: '演示账号' },
    ],
    form: {
      id: undefined as string | number | undefined,
      username: undefined as string | undefined,
      password: undefined as string | undefined,
      role: undefined as string | undefined,
      status: undefined as string | undefined,
      nickname: undefined as string | undefined,
      email: undefined as string | undefined,
      mobile: undefined as string | undefined,
      cover: undefined as File | undefined,
    },
    rules: {
      username: [{ required: true, message: '请输入用户名', trigger: 'change' }],
      password: [{ required: true, message: '请输入密码', trigger: 'change' }],
      nickname: [{ required: true, message: '请输入昵称', trigger: 'change' }],
      role: [{ required: true, message: '请选择角色', trigger: 'change' }],
      status: [{ required: true, message: '请选择状态', trigger: 'change' }],
      email: [{ type: 'email', message: '请输入有效的邮箱地址', trigger: 'change' }],
      mobile: [{ pattern: /^1[3-9]\d{9}$/, message: '请输入有效的手机号', trigger: 'change' }],
    },
  });

  const myform = ref<FormInstance>();

  onMounted(() => {
    getUserList();
  });

  const getUserList = () => {
    data.loading = true;
    listApi({
      keyword: data.keyword,
      page: data.page,
      pageSize: data.pageSize,
    })
      .then((res) => {
        data.loading = false;
        res.data.forEach((item: DataItem, index: number) => {
          item.index = index + 1;
        });
        data.userList = res.data;
      })
      .catch((err) => {
        data.loading = false;
        console.log(err);
      });
  };

  const onSearchChange = (e: Event) => {
    data.keyword = (e.target as HTMLInputElement).value;
  };

  const onSearch = () => {
    getUserList();
  };

  const rowSelection = ref({
    onChange: (selectedRowKeys: (string | number)[], selectedRows: DataItem[]) => {
      data.selectedRowKeys = selectedRowKeys;
    },
  });

  const handleAdd = () => {
    resetModal();
    modal.visible = true;
    modal.editFlag = false;
    modal.title = '新增';
    for (const key in modal.form) {
      modal.form[key] = undefined;
    }
  };

  const handleEdit = (record: DataItem) => {
    resetModal();
    modal.visible = true;
    modal.editFlag = true;
    modal.title = '编辑';
    for (const key in record) {
      modal.form[key] = record[key];
    }
  };

  const confirmDelete = (record: DataItem) => {
    deleteApi({ ids: record.id })
      .then(() => {
        message.success('删除成功');
        getUserList();
      })
      .catch((err) => {
        message.warn(err.msg || '操作失败');
      });
  };

  const handleBatchDelete = () => {
    if (data.selectedRowKeys.length <= 0) {
      message.warn('请勾选删除项');
      return;
    }
    deleteApi({ ids: data.selectedRowKeys.join(',') })
      .then(() => {
        message.success('删除成功');
        data.selectedRowKeys = [];
        getUserList();
      })
      .catch((err) => {
        message.warn(err.msg || '操作失败');
      });
  };

  const handleOk = () => {
    myform.value
      ?.validate()
      .then(() => {
        const formData = new FormData();
        Object.entries(modal.form).forEach(([key, value]) => {
          if (value !== undefined) {
            if (typeof value === 'number') {
              formData.append(key, value.toString()); // 处理 number 类型
            } else {
              formData.append(key, value); // 处理 string 或 Blob 类型
            }
          }
        });

        if (modal.editFlag) {
          updateApi({ id: modal.form.id }, formData)
            .then(() => {
              hideModal();
              getUserList();
            })
            .catch((err) => {
              message.warn(err.msg || '操作失败');
            });
        } else {
          createApi(formData)
            .then(() => {
              hideModal();
              getUserList();
            })
            .catch((err) => {
              message.warn(err.msg || '操作失败');
            });
        }
      })
      .catch(() => {
        message.warn('请填写完整信息');
      });
  };

  const handleCancel = () => {
    hideModal();
  };

  const resetModal = () => {
    myform.value?.resetFields();
  };

  const hideModal = () => {
    modal.visible = false;
  };

  const onChange = (current: number) => {
    data.page = current;
    getUserList();
  };
</script>

<style scoped lang="less">
  .page-view {
    min-height: 100%;
    background: #fff;
    padding: 24px;
    display: flex;
    flex-direction: column;
  }

  .table-operations {
    margin-bottom: 16px;
    text-align: right;
  }

  .table-operations > button {
    margin-right: 8px;
  }
</style>
